package com._520it.crm.web.controller;

import com._520it.crm.domain.Employee;
import com._520it.crm.domain.OrderBill;
import com._520it.crm.domain.UnderWriting;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.UnderWritingQueryObject;
import com._520it.crm.service.IOrderBillService;
import com._520it.crm.service.IUnderWritingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/pending_approval")
public class PendingApprovalController {

    @Autowired
    private IUnderWritingService underWritingService;
    @Autowired
    private IOrderBillService orderBillService;

    @RequestMapping("")
    public String index(){
        return "pending_approval";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(UnderWritingQueryObject qo){
        PageResult result = null;
        result = underWritingService.queryPage(qo);
        return result;
    }

    @RequestMapping("/receive")
    public String receive(Long holderId, Long orderBillId, Model model) {
        UnderWriting underWriting = underWritingService.selectByOrderBillId(orderBillId);
        if (underWriting == null){
            underWriting = new UnderWriting();
            OrderBill orderBill = new OrderBill();
            underWriting.setOrderBill(orderBill);
            Employee employee = new Employee();
            orderBill.setId(orderBillId);
            employee.setId(holderId);
            underWriting.setHolder(employee);
            underWriting.setStatus(UnderWriting.PENDING_APPROVAL);
            underWritingService.insert(underWriting);
        }
        model.addAttribute("orderBillId",orderBillId);
        model.addAttribute("underWritingId",underWriting.getId());
        return "underWriting";
    }

    @RequestMapping("/untread")
    @ResponseBody
    public AjaxResult untread(Long orderBillId){
        AjaxResult result = null;
        try {
            orderBillService.updateStatusById(orderBillId,6);
            result = new AjaxResult(true,"作废成功");
        } catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,e.getMessage());
        }
        return result;
    }
}
